package com.zp.pullloadhelper;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;

/**
 * Created by izp on 2017/5/3.
 */

public abstract class BaseActivity extends AppCompatActivity {

    private static final String S_FRAGMENT_TAG = "TAG";//四个fragment一个tag是不是有点问题啊
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base);
        if (savedInstanceState == null) {
            Fragment content = createFragment();
            Intent intent = getIntent();
            content.setArguments(intent.getExtras());
            getSupportFragmentManager().beginTransaction().add(R.id.content,content,S_FRAGMENT_TAG).commit();
        }
    }

    @Override
    public void onBackPressed() {
        boolean isBackPressHandled = false;
        Fragment fragment = getContentFragment();
        if (fragment instanceof HandleBackEvent){
            isBackPressHandled = ((HandleBackEvent) fragment).handleBack();
        }
        if (!isBackPressHandled){
            super.onBackPressed();
        }
    }

    public Fragment getContentFragment(){
        return getSupportFragmentManager().findFragmentByTag(S_FRAGMENT_TAG);
    }

    protected abstract Fragment createFragment();

}
